package com.lizardmind.haofudoctor.ui.diagnosis.diagnose.userFill

import android.arch.lifecycle.ViewModelProviders
import android.graphics.Bitmap
import android.os.Bundle
import android.view.View
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.EncodeUtils
import com.blankj.utilcode.util.ImageUtils
import com.linxiao.framework.activity.BaseActivity
import com.linxiao.framework.util.getBaseFragmentStatePagerAdapter
import com.linxiao.framework.util.ke
import com.linxiao.framework.util.toasts
import com.lizardmind.haofudoctor.R
import com.lizardmind.haofudoctor.manager.postApi
import com.lizardmind.haofudoctor.other.BaseDocumentsBean
import com.lizardmind.haofudoctor.other.DiagnoseDocumentsBean
import com.lizardmind.haofudoctor.other.DoctorInfo
import com.lizardmind.haofudoctor.other.Request
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.activity_fill_diagnose_two.*
import kotlinx.android.synthetic.main.include_title_text.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 *
 * Created by Extends on 2017/11/13 15:32
 */
class FillDiagnoseTwoActivity:BaseActivity() {
    private val list = arrayListOf<FillDiagnoseTwoFragment>()
    val bean by lazy { intent.getSerializableExtra("bean") as BaseDocumentsBean }
    val isClick by lazy { intent.getBooleanExtra("isClick",true) }
    override fun onCreatRootView(): View?{
        setContentView(R.layout.activity_fill_diagnose_two)
        return null
    }

    override fun onInitView(savedInstanceState: Bundle?) {
        //初始化标题栏
        imgLeft.setImageResource(R.mipmap.back)
        imgLeft.onClick { finish() }
        titleText.text = "问诊单"


        list.addAll(bean.problem.mapIndexed { index, it -> FillDiagnoseTwoFragment.getFragment(it,index,bean.problem.size) })

        afdt_hackVP.adapter = getBaseFragmentStatePagerAdapter(supportFragmentManager,list)
        afdt_hackVP.setScanScroll(false)
    }

    /**
     * 提交数据
     */
    fun submit(){
        val value = list.joinToString(",","[","]") { it.getAnswer() }
        val supplement = list.joinToString(","){ EncodeUtils.base64Encode2String(it.getSupplement().toByteArray())}
        val vm = ViewModelProviders.of(ActivityUtils.getActivityList()[ActivityUtils.getActivityList().lastIndex-1] as BaseActivity).get(FillDiagnoseOneVM::class.java)
        val tonguePicture = vm.tongue_picture.map { ImageUtils.bitmap2Bytes(ImageUtils.getBitmap(it.compressPath), Bitmap.CompressFormat.JPEG) }
                .joinToString(","){EncodeUtils.base64Encode2String(it)}
        val relatedPicture = vm.related_picture.map { ImageUtils.bitmap2Bytes(ImageUtils.getBitmap(it.compressPath), Bitmap.CompressFormat.JPEG) }
                .joinToString(","){EncodeUtils.base64Encode2String(it)}
        postApi<Request<String>>(
                hashMapOf("action" to "fill_diagnose",
                        "token" to DoctorInfo.token,
                        "id" to vm.bean.id,
                        "answer" to value,
                        "fill_uid" to vm.bean.candidate_info[vm.select.get()].uid,
                        "describe" to vm.describe.get(),
                        "supplement" to supplement,
                        "tongue_picture" to tonguePicture,
                        "related_picture" to relatedPicture),
                Consumer {
                    it.dispose {
                        toasts(it)
                        finish()
                    }
                },context = this
        )
    }
}